#!/bin/sh
#
# Draco Live
#
# Copyright 2008 Ole Andre Rodlie <olear@dracolinux.org>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS OFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF

NULL=/dev/null
LIVE=/.live
MNT=$LIVE/mnt
LIVECD=$MNT/boot
KEY=$LIVECD/dP
MOD=$LIVECD/modules
MODS="etc lib bin sbin usr boot var"
DRV="usbcore.ko ohci-hcd.ko uhci-hcd.ko ehci-hcd.ko usb-storage.ko squashfs.ko unionfs.ko"
CDDEV="/dev/hda /dev/hdb /dev/hdc /dev/hdd /dev/hde /dev/hdf /dev/hdg /dev/hdh /dev/sr0 /dev/sr1 /dev/sr2 /dev/sr3 /dev/sr4 /dev/sr5 /dev/sr6 /dev/sr7 /dev/sr8 /dev/sr9"
HDDEV="/dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1 /dev/sdf1 /dev/sdg1 /dev/sdh1 /dev/hda1 /dev/hdb1 /dev/hdc1 /dev/hdd1"

/mount -v proc /proc -n -t proc
/mount tmpfs /home -t tmpfs
/mount tmpfs /tmp -t tmpfs

for driver in $DRV; do
  if [ -f /lib/modules/$KERNEL/$driver ]; then
    /sbin/insmod $driver 1> $NULL 2> $NULL
  fi
done

/mount tmpfs $LIVE -t tmpfs
/bin/mkdir $MNT
/bin/cp /etc/fstab $LIVE/
/bin/cp /etc/rc.d/rc.local $LIVE/

for device in $CDDEV; do
  #echo "Checking CD/DVD ..."
  /mount -o ro -t iso9660 $device $MNT 1> $NULL 2> $NULL
  if [ $? = 0 ]; then      
    if [ -f $KEY ]; then
      echo > $LIVE/LIVE
      break
    else
      /umount $MNT
    fi
  fi
done

if [ ! -f $LIVE/LIVE ]; then
  #echo "Checking HD/USB ..."
  sleep 5
  for device in $HDDEV; do
    /mount -o ro $device $MNT 1> $NULL 2> $NULL
    if [ $? = 0 ]; then   
      if [ -f $KEY ]; then
        echo > $LIVE/LIVE
        break
      else
        /umount $MNT
      fi
    fi
  done
fi

if [ -f $LIVE/LIVE ]; then
  if [ -d $MOD ]; then
    for module in $MODS; do 
      /bin/mkdir $LIVE/$module.ro
      if [ -d $LIVE/$module.ro ]; then
        #echo "Mounting $module with squashfs ..."
        /mount -t squashfs -o loop $MOD/$module.mod $LIVE/$module.ro
        /bin/mkdir $LIVE/$module.rw
      fi
      if [ -d $LIVE/$module.rw ]; then
         #echo "Mounting $module with unionfs"
        /mount -t unionfs -o dirs=$LIVE/$module.rw=rw:$LIVE/$module.ro=ro unionfs /$module
      fi
    done
    /bin/mv $LIVE/fstab /etc/
    /bin/mv $LIVE/rc.local /etc/rc.d/
    if [ -f $LIVE/etc.ro/rc.d/rc.S ]; then
      #echo "Starting Draco ..."
      . /etc/rc.d/rc.S
    else
      echo
      echo "Draco Failsafe"
      echo
      /bin/sh
    fi
  else
  echo
   echo "Draco Failsafe"
   echo
    /bin/sh
  fi
else
  echo
  echo "Draco Failsafe"
  echo
  /bin/sh
fi
